//
//  XMbaseCanMoveView.m
//  BigJoy
//
//  Created by Rowling on 2017/6/16.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "XMbaseCanMoveView.h"



@interface XMbaseCanMoveView()
@property (nonatomic,strong)UIButton * cancelButton;
@property (nonatomic,strong) UIImageView *imageView;
@end

@implementation XMbaseCanMoveView

-(UIButton *)cancelButton {
    if (_cancelButton == nil) {
        _cancelButton = [[UIButton alloc]init];
    }
    return _cancelButton;
}

-(UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc]init];
    }
    return _imageView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
        [self addTap];
    }
    return self;
}

-(void)setImage:(UIImage *)image {
    _image = image;
    self.imageView.image = image;
}

-(void)layoutSubviews {
    [self setUI];
}

-(void)setUI {
    [self addSubview:self.imageView];
    self.imageView.clipsToBounds = YES;
    [self.cancelButton setImage:[UIImage imageNamed:@"guanbi"] forState:UIControlStateNormal];
    [self.cancelButton sizeToFit];
    self.cancelButton.x = self.bounds.size.width - self.cancelButton.width;
    self.cancelButton.y = 0;
    [self addSubview:self.cancelButton];
    self.imageView.frame = CGRectMake(1, 1 + Inset, self.bounds.size.width - 2 - Inset, self.bounds.size.height - 2 - Inset);
    [self.cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
}

-(void)cancel {
    [self.Delegate XMbaseCanMoveViewDeleButtonClick:self];
    [self removeFromSuperview];
}

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (self.isSelect) {
        UIBezierPath * bezer = [UIBezierPath bezierPathWithRect:CGRectMake(1, Inset + 1, rect.size.width - 2 - Inset, rect.size.height - 2 - Inset)];
        [XMColor(221, 221, 221) setStroke];
        [bezer stroke];
    } else {
        
    }
}

-(void)addTap {
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
    [self addGestureRecognizer:tap];
    self.tap = tap;
    [tap addTarget:self action:@selector(tapClick)];
}

-(void)tapClick {
    if ([self.Delegate respondsToSelector:@selector(XMbaseCanMoveViewTap:)]) {
        [self.Delegate XMbaseCanMoveViewTap:self];
    }
}

-(void)setIsSelect:(BOOL)isSelect {
    _isSelect = isSelect;
    self.cancelButton.hidden = !isSelect;
    [self setNeedsDisplay];
    
}

-(void)dealloc {
    NSLog(@"xmbasecanMoveViewDealloc");
}

@end
